import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { EqpInsurancesApi } from "@/api/erp/equipments/insurance";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
import { EquipmentsApi } from "@/api/erp/equipments/device";
let reload: Function;
let pkId: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage.equipment.insurance";
	const formSchema: FormSchema[] = [
		{
			label: t(`drugStorage.equipment.device.eqpName`),
			field: "eqpId",
			component: "ApiSelect",
			componentProps: {
				api: EquipmentsApi.getEquipmentsPage,
				labelField: "eqpName",
				valueField: "eqpId"
			}
		},
		{
			label: t(`${en_vi}.resprId`),
			field: "resprId",
			component: "Input"
		},
		{
			label: t(`${en_vi}.insurDtimeBuy`),
			field: "insurDtimeBuy",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.insurDtimeFrom`),
			field: "insurDtimeFrom",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.remindDtime`),
			field: "remindDtime",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.insurDtimeTo`),
			field: "insurDtimeTo",
			component: "DatePicker"
		},
		// {
		// 	label: t("felix_ui.status"),
		// 	component: "Select",
		// 	field: "status",
		// 	componentProps: {
		// 		options: [
		// 			{ label: t(`drugStorage.notStarted`), value: "-1" },
		// 			{ label: t(`drugStorage.inApproval`), value: "1" },
		// 			{ label: t(`drugStorage.approved`), value: "2" },
		// 			{ label: t(`drugStorage.approvalNotApproved`), value: "3" },
		// 			{ label: t(`drugStorage.cancelled`), value: "4" }
		// 		]
		// 	}
		// },
		{
			label: t(`${en_vi}.insurCompId`),
			field: "insurCompId",
			component: "Input"
		},
		{
			label: t(`${en_vi}.insurCompName`),
			field: "insurCompName",
			component: "Input"
		},
		// {
		// 	label: t(`${en_vi}.insurType`),
		// 	field: "insurType",
		// 	component: "Select",
		// 	componentProps: {
		// 		// 需要字典生成
		// 		options: [
		// 			{ label: t(`drugStorage.inventoryManagement.otherInboundItems.audited`), value: "20" },
		// 			{ label: t(`drugStorage.inventoryManagement.otherInboundItems.unaudited`), value: "10" }
		// 		]
		// 	}
		// },
		{
			label: t(`${en_vi}.insurAmount`),
			field: "insurAmount",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.insurMemo`),
			field: "insurMemo",
			component: "Input"
		},
		{
			label: t(`${en_vi}.certTag`),
			field: "certTag",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`drugStorage.yes`), value: "1" },
					{ label: t(`drugStorage.no`), value: "0" }
				]
			}
		},
		{
			label: t(`${en_vi}.certAlbumId`),
			field: "certAlbumId",
			component: "InputTextArea"
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t(`felix_ui.edit`) : t(`felix_ui.add`);
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		pkId = data.record?.pkId;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.insurDtimeBuy = new Date(formValue.insurDtimeBuy).getTime();
		formValue.insurDtimeFrom = new Date(formValue.insurDtimeFrom).getTime();
		formValue.remindDtime = new Date(formValue.remindDtime).getTime();
		formValue.insurDtimeTo = new Date(formValue.insurDtimeTo).getTime();
		let res;
		if (isUpdated) {
			formValue.pkId = pkId;
			res = await EqpInsurancesApi.updateEqpInsurances(formValue);
		} else {
			res = await EqpInsurancesApi.createEqpInsurances(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	return (
		<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
